/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.FileReader;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FileGenUtil
/*     */ {
/*     */   private static final String BEGIN_MARKER = "BEGIN-AUTO-GEN-REPLACE";
/*     */   private static final String END_MARKER = "END-AUTO-GEN-REPLACE";
/*     */   
/*     */   public static String wrapComments(String comments, int maxLineLength, String prefix)
/*     */   {
/*  40 */     return wrapComments(comments, maxLineLength, prefix, null);
/*     */   }
/*     */   
/*     */   public static String genDoNotModifyDisclaimer(String prefix, String generatedBy) {
/*  44 */     return genDoNotModifyDisclaimer(prefix, generatedBy, null);
/*     */   }
/*     */   
/*     */   public static String genDoNotModifyDisclaimer(String prefix, String generatedBy, String version) {
/*  48 */     StringBuilder result = new StringBuilder();
/*     */     
/*  50 */     result.append(prefix + "\n");
/*  51 */     result.append(prefix + "\n");
/*  52 */     result.append(prefix + " DO NOT MODIFY - generated by " + generatedBy + "\n");
/*  53 */     result.append(prefix + " " + (version == null ? BuildInfo.FULL_VERSION : version) + "\n");
/*  54 */     result.append(prefix + "\n");
/*  55 */     result.append(prefix + "\n");
/*     */     
/*  57 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static String wrapComments(String comments, int maxLineLength, String prefix, String suffix) {
/*  61 */     comments = comments.trim().replaceAll("\\s+", " ");
/*  62 */     StringBuilder result = new StringBuilder();
/*  63 */     String[] words = comments.split("\\s+");
/*  64 */     int lineLength = 0;
/*  65 */     for (String word : words) {
/*  66 */       if (lineLength + word.length() + 1 > maxLineLength) {
/*  67 */         if (suffix != null) {
/*  68 */           result.append(suffix);
/*     */         }
/*  70 */         result.append("\n");
/*     */         
/*  72 */         lineLength = 0;
/*     */       }
/*  74 */       if ((lineLength == 0) && (prefix != null)) {
/*  75 */         result.append(prefix);
/*     */       }
/*  77 */       if (lineLength > 0) {
/*  78 */         result.append(' ');lineLength++;
/*     */       }
/*  80 */       result.append(word);
/*  81 */       lineLength += word.length();
/*     */     }
/*  83 */     if ((result.length() == 0) && (prefix != null)) {
/*  84 */       result.append(prefix);
/*     */     }
/*  86 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static void replaceJavaFile(String javaFile, String content) throws IOException
/*     */   {
/*  91 */     BufferedReader in = null;
/*  92 */     BufferedWriter out = null;
/*     */     
/*  94 */     File oldFile = new File(javaFile);
/*  95 */     if (!oldFile.canWrite()) {
/*  96 */       System.err.println("============================================");
/*  97 */       System.err.println("Unable to write to: " + javaFile);
/*  98 */       System.err.println("============================================");
/*  99 */       System.exit(1);
/*     */     }
/*     */     
/* 102 */     File newFile = new File(javaFile + "-autogen");
/*     */     try
/*     */     {
/* 105 */       out = new BufferedWriter(new FileWriter(newFile));
/* 106 */       in = new BufferedReader(new FileReader(oldFile));
/*     */       
/* 108 */       boolean replaceMode = false;
/*     */       String line;
/* 110 */       while ((line = in.readLine()) != null) {
/* 111 */         if (line.indexOf("BEGIN-AUTO-GEN-REPLACE") != -1) {
/* 112 */           out.write(line);
/* 113 */           out.newLine();
/* 114 */           out.write(content);
/* 115 */           out.newLine();
/* 116 */           replaceMode = true;
/* 117 */         } else if (line.indexOf("END-AUTO-GEN-REPLACE") != -1) {
/* 118 */           replaceMode = false;
/* 119 */           out.write(line);
/* 120 */           out.newLine();
/* 121 */         } else if (!replaceMode) {
/* 122 */           out.write(line);
/* 123 */           out.newLine();
/*     */         }
/*     */       }
/*     */       
/* 127 */       in.close();
/* 128 */       in = null;
/*     */       
/* 130 */       out.close();
/* 131 */       out = null;
/*     */       
/* 133 */       if (!newFile.renameTo(oldFile)) {
/* 134 */         System.err.println("============================================");
/* 135 */         System.err.format("Unable to rename(%s) to (%s)%n", new Object[] { newFile.getName(), oldFile });
/* 136 */         System.err.println("============================================");
/* 137 */         System.exit(1);
/*     */       }
/*     */       
/* 140 */       System.out.println("======================================");
/* 141 */       System.out.println("generated: " + javaFile);
/* 142 */       System.out.println("======================================");
/*     */     }
/*     */     finally {
/* 145 */       if (in != null) in.close();
/* 146 */       if (out != null) out.close();
/*     */     }
/*     */   }
/*     */   
/*     */   public static void replaceFile(String outFile, String templateFile, Map<String, String> templateFillers) throws IOException
/*     */   {
/* 152 */     byte[] templateBytes = ByteUtil.getContent(new File(templateFile));
/* 153 */     String templateString = new String(templateBytes, "utf-8");
/*     */     
/* 155 */     String content = StringUtil.fillTemplate(templateString, templateFillers);
/*     */     
/* 157 */     writeToFileFile(outFile, content);
/*     */   }
/*     */   
/*     */   public static void replaceFile(String outFile, String content) throws IOException {
/* 161 */     writeToFileFile(outFile, content);
/*     */   }
/*     */   
/*     */   private static void writeToFileFile(String outFile, String content) throws IOException
/*     */   {
/* 166 */     File oldFile = new File(outFile);
/* 167 */     if (!oldFile.canWrite()) {
/* 168 */       System.err.println("============================================");
/* 169 */       System.err.println("Unable to write to: " + outFile);
/* 170 */       System.err.println("============================================");
/* 171 */       System.exit(1);
/*     */     }
/*     */     
/* 174 */     BufferedWriter out = null;
/* 175 */     File newFile = new File(outFile + "-autogen");
/*     */     try
/*     */     {
/* 178 */       out = new BufferedWriter(new FileWriter(newFile));
/* 179 */       out.write(content);
/*     */       
/* 181 */       out.close();
/* 182 */       out = null;
/*     */       
/* 184 */       if (!newFile.renameTo(oldFile)) {
/* 185 */         System.err.println("============================================");
/* 186 */         System.err.format("Unable to rename(%s) to (%s)%n", new Object[] { newFile.getName(), oldFile });
/* 187 */         System.err.println("============================================");
/* 188 */         System.exit(1);
/*     */       }
/*     */       
/* 191 */       System.out.println("======================================");
/* 192 */       System.out.println("generated: " + outFile);
/* 193 */       System.out.println("======================================");
/*     */     }
/*     */     finally {
/* 196 */       if (out != null) out.close();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/FileGenUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */